package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.infrastructure.repository.condition.RuleCondition;
import com.spa.infrastructure.repository.mapper.RuleMapper;
import com.spa.infrastructure.repository.po.RulePO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class RuleRepositoryService extends ServiceImpl<RuleMapper, RulePO> {

    public Page<RulePO> pageList(RuleCondition condition) {
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper(condition));
    }

    public List<RulePO> list(RuleCondition condition) {
        return list(wrapper(condition));
    }

    public boolean addInfo(RulePO po) {
        return save(po);
    }

    public boolean updateInfo(RulePO po) {
        return updateById(po);
    }

    private LambdaQueryWrapper<RulePO> wrapper(RuleCondition condition) {
        LambdaQueryWrapper<RulePO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getRelevanceId()), RulePO::getRelevanceId, condition.getRelevanceId());
        wrapper.in(CollUtil.isNotEmpty(condition.getRelevanceIds()),RulePO::getRelevanceId,condition.getRelevanceIds());
        return wrapper;
    }

    //private LambdaUpdateWrapper<ShopPO> updateWrapper(ShopPO po) {
    //    LambdaUpdateWrapper<ShopPO> wrapper = Wrappers.lambdaUpdate();
    //    wrapper.eq(ObjectUtil.isNotNull(po.getId()), ShopPO::getId, po.getId());
    //    wrapper.set(StringUtils.hasText(po.getPhone()), ShopPO::getPhone, po.getPhone());
    //    wrapper.set(StringUtils.hasText(po.getAddress()), ShopPO::getAddress, po.getAddress());
    //    wrapper.set(StringUtils.hasText(po.getLogo()), ShopPO::getLogo, po.getLogo());
    //    wrapper.set(StringUtils.hasText(po.getBusinessLicense()), ShopPO::getBusinessLicense, po.getBusinessLicense());
    //    wrapper.set(StringUtils.hasText(po.getRemark()), ShopPO::getRemark, po.getRemark());
    //    wrapper.set(StringUtils.hasText(po.getOwner()), ShopPO::getOwner, po.getOwner());
    //    wrapper.set(ObjectUtil.isNotNull(po.getIsDel()), ShopPO::getIsDel, po.getIsDel());
    //    wrapper.set(ObjectUtil.isNotNull(po.getCityId()), ShopPO::getCityId, po.getCityId());
    //    wrapper.set(StringUtils.hasText(po.getName()), ShopPO::getName, po.getName());
    //    wrapper.set(ObjectUtils.isNotEmpty(po.getWithdrawPeriod()), ShopPO::getWithdrawPeriod, po.getWithdrawPeriod());
    //    return wrapper;
    //}
}
